#include "cpu/exec/template-start.h"

make_helper(je_si_b) {
	int len = decode_si_b(eip + 1);
	// Log("op_src->val = %x", op_src->val);
	if(cpu.eflags.ZF == 1) {
		cpu.eip += (int8_t)op_src->val;
		
	}
	//如果不跳转，那么就继续执行下一条指令
	return len + 1;
}

make_helper(jne_si_b) {
	int len = decode_si_b(eip + 1);
	if(cpu.eflags.ZF == 0) {
		cpu.eip += (int8_t)op_src->val;
		// return len + 1;
	}
	return len + 1;
}

//0x7e
make_helper(jle_si_b) {
	int len = decode_si_b(eip + 1);
	if(cpu.eflags.ZF == 1 || cpu.eflags.SF != cpu.eflags.OF) {
		cpu.eip += (int8_t)op_src->val;
		// return len + 1;
		// return 0;
	}
	return len + 1;
}

//0x99
make_helper(jmp_rm_r) {
    //解释modrm
    // ModR_M m;
    // m.val = instr_fetch(eip + 1, 1);
    //分析modrm
	int len = decode_rm_w(eip + 1);
	Log("len = %d", len);
	//
	Log("op_src->type = %d", op_src->type);
	Log("cpu.eax = %x", cpu.eax);
	Log("op_src->reg = %d", op_src->reg);
	// assert(0);
	if (op_src->type == OP_TYPE_REG) {
		switch (op_src->reg) {
			case 0:
				cpu.eip = cpu.eax;
				break;
			case 1:
				cpu.eip = cpu.ecx;
				break;
			case 2:
				cpu.eip = cpu.edx;
				break;
			case 3:
				cpu.eip = cpu.ebx;
				break;
			case 4:
				cpu.eip = cpu.esp;
				break;
			case 5:
				cpu.eip = cpu.ebp;
				break;
			case 6:
				cpu.eip = cpu.esi;
				break;
			case 7:
				cpu.eip = cpu.edi;
				break;
			default:
				assert(0);
		}
	}
	else if (op_src->type == OP_TYPE_MEM) {
		cpu.eip = hwaddr_read(op_src->addr, 4);
	}
	else {
		assert(0);
	}
	return 0;
    
}

#include "cpu/exec/template-end.h"